<template>
  <div class="form">
    <icon-close class="close" @click="$router.push('/')" />
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
      <!-- 登录 -->
      <el-tab-pane label="User" name="first">
        <el-form
          ref="ruleForm1Ref"
          :model="ruleForm1"
          status-icon
          :rules="rules_login"
          label-width="120px"
          class="demo-ruleForm1"
        >
          <el-form-item label="账号" prop="phone">
            <el-input v-model.number="ruleForm1.phone" placeholder="手机号码" />
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              v-model="ruleForm1.pass"
              type="password"
              autocomplete="off"
              placeholder="密码"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="Login(ruleForm1Ref)"
              >登录</el-button
            >
          </el-form-item>
          <div class="tips">
            <span @click="$router.push('/register')"
              >还没有加入我们？前往注册-></span
            >
          </div>
        </el-form>
      </el-tab-pane>

      <!-- 注册 -->
      <el-tab-pane label="Config" name="second">
        <el-form
          ref="ruleForm2Ref"
          :model="ruleForm2"
          :rules="rules_register"
          label-width="15%"
          class="demo-ruleForm2"
          :size="formSize"
          status-icon
        >
          <el-form-item label="昵称" prop="name">
            <el-input v-model="ruleForm2.name" placeholder="3-16个字符" />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="ruleForm2.gender">
              <el-radio label="男" value="1" />
              <el-radio label="女" value="0" />
            </el-radio-group>
          </el-form-item>
          <el-form-item label="生日" prop="birthday">
            <el-date-picker
              v-model="ruleForm2.birthday"
              type="date"
              label="出生日期"
              placeholder="选择你的出生日期"
              style="width: 61%"
            />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="ruleForm2.email"
              placeholder="请输入你的真实邮箱"
            />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="ruleForm2.password"
              type="password"
              autocomplete="off"
              placeholder="长度在6-16位,建议使用至少两种字符"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleForm2Ref)"
              >确认注册</el-button
            >
            <el-button @click="resetForm(ruleForm2Ref)">重置信息</el-button>
          </el-form-item>
          <div class="tips">
            <span @click="$router.push('/login')">已有账号？前往登录-></span>
          </div>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance, TabsPaneContext, FormRules } from "element-plus";

//注册

const formSize = ref("default");
const ruleForm2Ref = ref<FormInstance>();
const ruleForm2 = reactive({
  name: "",
  gender: "",
  birthday: "",
  email: "",
  password: "",
});

const rules_register = reactive<FormRules>({
  name: [
    { required: true, message: "昵称不能为空", trigger: "blur" },
    { min: 3, max: 16, message: "长度应该在3-16个字符", trigger: "blur" },
  ],
  gender: [
    {
      required: true,
      message: "请选择性别",
      trigger: "change",
    },
  ],
  birthday: [
    {
      type: "date",
      required: true,
      message: "请填写正确的日期格式",
      trigger: "change",
    },
  ],
  email: [
    { required: true, message: "邮箱不能为空", trigger: "blur" },
    { type: "email", message: "邮箱不存在", trigger: "blur" },
  ],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 6, max: 16, message: "长度应该在6-16个字符", trigger: "blur" },
  ],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

//登录
const activeName = ref("first");

const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event);
};
const ruleForm1Ref = ref<FormInstance>();
//表单数据
const ruleForm1 = reactive({
  phone: "",
  pass: "",
});

//校验账号
const checkPhone = (rule: any, value: any, callback: any) => {
  if (!value) {
    return callback(new Error("账号不能为空"));
  }
  setTimeout(() => {
    if (
      !/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
        value
      )
    ) {
      callback(new Error("请输入正确的手机号码"));
    } else {
      callback();
    }
  }, 1000);
};

//登录
const Login = (formEl: FormInstance | undefined) => {};

//定义校验函数
const rules_login = reactive({
  phone: [{ validator: checkPhone, trigger: "blur" }],
});
</script>

<style lang="scss" scoped>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.form {
  position: fixed;
  left: 25%;
  right: 35%;
  top: 10%;

  .close {
    float: right;
    padding: 0.05rem;
  }

  form {
    padding: 0.2rem;
    border-radius: 0.1rem;
    background-color: rgb(246, 246, 246);

    .tips {
      color: green;
      float: right;
      font-size: 0.05rem;
    }
  }
}

span {
  cursor: pointer;
}
</style>
